package com.pj.common.base

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.pj.common.databinding.LayoutPullRecyclerviewBinding
import com.qmuiteam.qmui.recyclerView.QMUIRVDraggableScrollBar
import com.qmuiteam.qmui.widget.section.QMUIStickySectionLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


interface IPullRecyclerLayout : OnRefreshLoadMoreListener {

    fun setup(binding: LayoutPullRecyclerviewBinding) {
        setupRefreshLayout(binding.refreshLayout)
        setupRecyclerView(binding.recyclerView)
    }


    /**
     * 下拉刷新,上拉加载的回调
     * @param pullToRefresh 下拉刷新true/上拉加载false
     */
    fun loadData(pullToRefresh: Boolean) {

    }

    /**
     *  开始自动加载数据
     */
    fun initLoadData() = true

    /**
     * 是否开启下拉刷新,默认true
     */
    fun enableRefresh() = true

    /**
     * 是否开启上拉加载,默认true
     */
    fun enableLoadMore() = true

    /**
     * 刷新列表数据
     */
    fun refreshData(flag: Boolean) {
        loadData(flag)
    }


    private fun setupRefreshLayout(refreshLayout: RefreshLayout) {
        refreshLayout.apply {
            //是否在列表不满一页时候开启上拉加载功能
            setEnableLoadMoreWhenContentNotFull(false)
            //是否启用越界拖动（仿苹果效果）
            //setEnableOverScrollDrag(enableOverScrollDrag())
            setEnableRefresh(enableRefresh())
            setEnableLoadMore(enableLoadMore())
            setOnRefreshLoadMoreListener(this@IPullRecyclerLayout)
        }
    }

    private fun setupRecyclerView(recyclerView: RecyclerView) {
        recyclerView.apply {
            setHasFixedSize(true)
        }
    }

    fun setStickLayout(recyclerView: RecyclerView,stickyLayout: QMUIStickySectionLayout?) {
        val scrollBar = QMUIRVDraggableScrollBar(0, 0, 0).apply {
            isEnableScrollBarFadeInOut = false
            attachToRecyclerView(recyclerView)
        }
        scrollBar.attachToStickSectionLayout(stickyLayout)
    }

    /**
     * 多状态和刷新控件的事件观察
     */
    fun subscribeMultiRefreshUi(
        vm: BaseViewModel,
        binding: LayoutPullRecyclerviewBinding,
        owner: LifecycleOwner
    ) {
        vm.apply {
            uiEmpty.observe(owner) {
                LogUtils.e("uiEmpty：监听值$it")
                binding.includeEmptyView.emptyView.apply {
                    if (it.isNullOrEmpty()) hide() else show(false, "提示", it, "重新加载") {
                        refreshData(
                            true
                        )
                    }
                }
            }

            hasMore.observe(owner) {
                if (it) {
                    //恢复没有更多数据的原始状态
                    binding.refreshLayout.setNoMoreData(false)
                    LogUtils.e("refresh：恢复没有更多数据的原始状态")
                } else {
                    //完成加载并标记没有更多数据
                    binding.refreshLayout.finishLoadMoreWithNoMoreData()
                    LogUtils.e("refresh：完成加载并标记没有更多数据")
                }
            }
        }

        if (initLoadData()) binding.refreshLayout.autoRefresh()
    }

    /**
     * 启用/禁用下拉刷新、上拉加载
     * @param isRefresh
     * @param isLoad
     */
    fun enableRefreshAndLoadMore(refreshLayout: RefreshLayout,isRefresh: Boolean = true, isLoad: Boolean = true) {
        refreshLayout.apply {
            setEnableRefresh(isRefresh)
            setEnableLoadMore(isLoad)
        }
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        refreshData(false)
        refreshLayout.finishLoadMore(800)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        refreshData(true)
        refreshLayout.finishRefresh(800)
    }
}